/**
 * @projectName study-note
 * @package Singleton
 * @className Singleton.Singleton02
 */
package Singleton;

/**
 * Singleton02
 * @description 单例模式--懒汉式(DCL 双重检查)
 * 优点：需要时实例化
 * 缺点：有点复杂
 * @author answer_wx
 * @date 2022/7/4 14:38
 * @version 1.0
 */
public class Singleton02 {
    private static volatile Singleton02 INSTANCE;

    private Singleton02() {
    }

    /**
     * 双重检查，保证线程安全
     * @return
     */
    public static Singleton02 getInstance() {
        if (INSTANCE == null) {
            synchronized (Singleton02.class) {
                if (INSTANCE == null) {
                    INSTANCE = new Singleton02();
                }
            }
        }
        return INSTANCE;
    }
}